package tn.orange.canreminder.comparator;

import java.util.ArrayList;
import java.util.List;

import tn.orange.canreminder.model.Competition;
import tn.orange.canreminder.model.Country;
import tn.orange.canreminder.model.Poule;
import tn.orange.canreminder.singleton.CountrySingleton;
import tn.orange.canreminder.singleton.PouleSingleton;

public class CompetitionHelper {

	private static List<Country> countries;
	private static List<Poule> poules;

	public static List<Competition> getCompetitionbyEquipe(List<String> values,
			List<Competition> competitions) {
		countries = CountrySingleton.getInstance();

		List<Competition> newCompetitions = new ArrayList<Competition>();
		for (Competition competition : competitions) {
			Country country1 = countries.get(getCountryPosition(competition
					.getCountryone_id()));
			Country country2 = countries.get(getCountryPosition(competition
					.getCountrytwo_id()));

			if (values.contains(country1.getName())
					|| values.contains(country2.getName())) {
				newCompetitions.add(competition);
			}
		}
		return newCompetitions;

	}

	public static List<Competition> getCompetitionbyPoule(List<String> values,
			List<Competition> competitions) {
		poules = PouleSingleton.getInstance();
		List<Competition> newCompetitions = new ArrayList<Competition>();
		for (Competition competition : competitions) {
			Poule poule = poules
					.get(getPoulePosition(competition.getPoule_id()));

			if (values.contains(poule.getName())) {
				newCompetitions.add(competition);
			}
		}
		return newCompetitions;

	}

	private static int getCountryPosition(int id) {
		int i = 0;
		for (Country country : countries) {

			if (country.getId() == id)
				return i;
			i++;
		}
		return -1;
	}

	private static int getPoulePosition(int id) {
		int i = 0;
		for (Poule poule : poules) {

			if (poule.getId() == id)
				return i;
			i++;
		}
		return -1;
	}
}